package com.hina.rec.application.manager.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hina.rec.domain.mapper.MenuMapper;
import com.hina.rec.domain.mapper.RoleMenuMapper;
import com.hina.rec.domain.model.MenuDO;
import com.hina.rec.domain.model.RoleMenuDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 菜单管理Manager
 *
 * @author liaosp
 * @date 2024-01-09
 */
@Component
public class MenuManager extends ServiceImpl<MenuMapper, MenuDO> {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    public List<MenuDO> getMenusByRoleIds(List<Integer> roleIds) {
        if (CollUtil.isEmpty(roleIds)) {
            return List.of();
        }
        List<Integer> menuIds = roleMenuMapper
                .selectList(Wrappers.<RoleMenuDO>lambdaQuery().in(RoleMenuDO::getRoleId, roleIds))
                .stream()
                .map(RoleMenuDO::getMenuId)
                .toList();
        if (CollUtil.isEmpty(menuIds)) {
            return List.of();
        }
        return listByIds(menuIds);
    }
}